// app.js
let count=0
App({
	globalData: {
		cookies: {}
	},

	// 统一处理 Cookie
	syncCookies(res) {
		const cookies = res.cookies || []; //["HWWAFSESID=86a88a928433648cd5; path=/", "HWWAFSESTIME=1738062329332; path=/"]
		const cookieMap = this.globalData.cookies;

		cookies.forEach(cookie => {
			const [keyValue] = cookie.split(';');
			const [key, value] = keyValue.split('=');
			if (key && value) {
				cookieMap[key.trim()] = value.trim();
			}
		});
		console.log(++count, cookies, this.globalData.cookies)
	},

	// 获取 Cookie 字符串
	getCookieHeader() {
		return Object.entries(this.globalData.cookies)
			.map(([k, v]) => `${k}=${v}`)
			.join('; ');
	},
	// 增加过期时间处理（需配合解析 Set-Cookie）
	parseCookie(cookieStr) {
		const parts = cookieStr.split(';').reduce((acc, part) => {
			const [key, val] = part.split('=');
			acc[key.trim().toLowerCase()] = val?.trim();
			return acc;
		}, {});

		return {
			name: Object.keys(parts)[0],
			value: parts[Object.keys(parts)[0]],
			expires: parts['max-age'] ? Date.now() + parseInt(parts['max-age']) * 1000 : null,
			domain: parts.domain,
			path: parts.path || '/'
		};
	}
});